<?php 


function search_results($keywords) {
  $returned_results = array();
  $returned_resultones = array();
  $returned_resulttwos = array();
  $where = "";
  $whereone = "";
  $whereimage = "";
  
  $keywords = preg_split('/[\s]+/', $keywords);
  $total_keywords = count($keywords);
  
  foreach($keywords as $key=>$keyword) {
    $where .= "`name` LIKE '%$keyword%'";
    if ($key != ($total_keywords - 1)) {
      $where .= " AND ";
    } 
    //print_r($where);
  }
  
  
    
  //echo $where;
  
  //$results = "SELECT `title`, LEFT(`description`, 70) as `description`, `url` FROM `articles` WHERE $where";
  $cat = array();
  
  $results = "SELECT `name` FROM `catalog` WHERE $where";
 
  $results_num = ($results = mysql_query($results)) ? mysql_num_rows($results) : 0;
  
  if ($results_num === 0) {
    foreach($keywords as $key=>$keyword) {
    $whereimage .= "`image_keywords` LIKE '%$keyword%' AND `user_id` = ".$_SESSION['user_id']."";
    if ($key != ($total_keywords - 1)) {
      $whereimage .= " AND ";
    } 
  }
  
  $imageR = array();
  
  $results = "SELECT * FROM `imagesVU` WHERE $whereimage";
  $results_num = ($results = mysql_query($results)) ? mysql_num_rows($results) : 0;
  
  if ($results_num === 0) {
   return false;
  } else {
    
    while ($imageR_row = mysql_fetch_assoc($results)) {
      $returned_results[] = array(
                          'userid' => $imageR_row['user_id'],
                          'imageid' => $imageR_row['image_id'],
				                  'album' => $imageR_row['album_id'],
				                  'timestamp' => $imageR_row['timestamp'],
				                  'ext' => $imageR_row['ext'],
				                  'keywords' => $imageR_row['image_keywords']
      );
    }
    return $returned_results;
  }
 

  } else {
    
    while ($cat_row = mysql_fetch_assoc($results)) {
      $returned_results[] = array('name' => $cat_row['name']);
   
   }
     
  
 
  $results = $returned_results;
  
  
  foreach ($results as $result)
  {
    $resultones = $result['name'];
  }
  
  
  $resultones = preg_split('/[\s]+/', $resultones);
  $total_resultones = count($resultones);
  
  //echo $total_resultones;
 
  foreach($resultones as $res=>$resultone) {
    $whereone .= "`catalog` LIKE '%$resultone%'";
    if ($res != ($total_resultones - 1)) {
      $whereone .= " AND ";
    } 
    //print_r($whereone);
  }
  
  $keyone = array();
  
  $result_query = "SELECT * FROM `keywords` WHERE $whereone";
  $result_query_num = ($result_query = mysql_query($result_query)) ? mysql_num_rows($result_query) : 0;
  
  
  
  if ($result_query_num === 0) {
   //return false;
  } else {
    
    while ($keyone_row = mysql_fetch_assoc($result_query)) {
      $returned_resultones[] = array('name' => $keyone_row['name']);
   
   }
   //print_r($returned_resultones);  
  }
   
  
  $results = $returned_resultones;
  $results_count = count($results);
  
  
  
  foreach ($results as $res=>$result)
  {
    $resulttwos .= $result['name'];
    if ($res != ($results_count - 1)) {
      $resulttwos .= " ";
    } 
  }

  $resulttwos = preg_split('/[\s]+/', $resulttwos);
  $total_resulttwos = count($resulttwos);
  $wheretwo = array();
    $imageR = array();
  //print_r($resulttwos);
  
  foreach($resulttwos as $resulttwo) {
    $wheretwo = "`image_keywords` LIKE '%$resulttwo%' AND `user_id` = ".$_SESSION['user_id']."";
    /*if ($res != ($total_resulttwos - 1)) {
      $wheretwo .= " AND ";
    } */
     $result_image = "SELECT * FROM `imagesVU` WHERE $wheretwo";
     $result_image_num += ($result_image = mysql_query($result_image)) ? mysql_num_rows($result_image) : 0;
     
     while ($imageR_row = mysql_fetch_assoc($result_image)) {
      $returned_resulttwos[] = array(
                          'userid' => $imageR_row['user_id'],
                          'imageid' => $imageR_row['image_id'],
				                  'album' => $imageR_row['album_id'],
				                  'timestamp' => $imageR_row['timestamp'],
				                  'ext' => $imageR_row['ext'],
				                  'keywords' => $imageR_row['image_keywords']
      );
    }
  }
   

  
  if ($result_image_num === 0) {
   return false;
  } else {
    
    
   return  $returned_resulttwos;
  }
 }

}

?>

